
#pragma once
#ifndef TRANSFER_HPP
#define TRANSFER_HPP

#include "common_include.hpp"
#include "camera.hpp"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>


#pragma pack(1)


typedef struct Receive_data{
    uint16_t imu_sample_time;
    uint8_t pic_index;
    uint16_t pic_sample_time;
    int16_t GYRO[3];
    int16_t ACC[3];
    float q[4];
}ReceData;


union ReceData_Union{
    uint8_t data[33];
    // uint8_t data[30];
    ReceData recedata_struct;
};

#pragma pack()

enum RECE_STATUS {INIT=0,HEAD1,HEAD2,DATA,TAIL1,TAIL2};

class Transfer{
    public:
        EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
        typedef std::shared_ptr<Transfer> Ptr;
        Transfer();
        ~Transfer();

        int TransferInit(int fd, int nSpeed, int nBits, char nEvent, int nStop);
        void TransferStop();
        //TransferInit(fd,115200,8,'N',1);
        int fd;
    private:
        void TransferLoop();
        
        std::thread transfer_thread_;
        std::mutex data_mutex_;
        std::atomic<bool> transfer_running_;


        ofstream outfile;

       
        uint8_t receive_buffer[30];
        uint8_t transfer_buffer[30];
        ReceData_Union recedata;
        enum RECE_STATUS rece_status;

};



#endif